package com.zhuky.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;

/**
 *
 *  邮件服务
 * @author zhuky
 *
 */

@Slf4j
@Service
public class MailService {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender sender;

    /**
     * 发送简单邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendSimpleMail(String to, String subject, String content){

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);
        sender.send(message);
    }

    /**
     * 发送Html富文本邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String to, String subject, String content){
        log.info("发送邮件开始:{},{},{}",to , subject, content);
        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(message, true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            mimeMessageHelper.setFrom(from);
            sender.send(message);

        } catch (MessagingException e) {
            log.error("发送邮件失败:{}",e);
        }
        log.info("发送邮件成功!");
    }

    /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePaths
     */

    public void sendAttachmentsMail(String to, String subject, String content, String[] filePaths) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);

        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(content, true);
        mimeMessageHelper.setFrom(from);
        /**
         * 读取和添加文件
         */
        for (String filePath : filePaths) {
            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            String fileName = fileSystemResource.getFilename();
            mimeMessageHelper.addAttachment(fileName, fileSystemResource);
        }
        sender.send(message);

    }

    /**
     * 发送正文带图片的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */

    public void sendInLineResourceMail(String to, String subject, String content,
                                       String rscPath, String rscId) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);

        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(content, true);
        mimeMessageHelper.setFrom(from);
        /**
         * 读取和添加图片
         */

        FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
        mimeMessageHelper.addInline(rscId , fileSystemResource);

        sender.send(message);

    }
    public void sendInLineResourceMail2(String to, String subject, String content,
                                        Map<String, String> picMap) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);

        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(content, true);
        mimeMessageHelper.setFrom(from);
        /**
         * 读取和添加图片
         */
        picMap.entrySet().forEach(entry ->{
            String rscPath = entry.getValue();
            FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
            try {
                mimeMessageHelper.addInline(entry.getKey() , fileSystemResource);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });

        sender.send(message);

    }
}
